package cn.itcast.music.ui.history;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import cn.itcast.music.databinding.ActivityHistoryBinding;
import cn.itcast.music.util.MusicUtil;

public class HistoryActivity extends AppCompatActivity {

    private ActivityHistoryBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHistoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initViewData();
        initViewAction();
    }

    private void initViewData() {
        MusicHistoryAdapter musicHistoryAdapter = new MusicHistoryAdapter(
                MusicUtil.getReadonlyMusicHistories(), this);
        binding.rvHistory.setAdapter(musicHistoryAdapter);
        binding.rvHistory.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initViewAction() {
        binding.tvClear.setOnClickListener(view -> {
            Log.d("App-HistoryActivity", "用户清除音乐历史记录");
            Toast.makeText(this, "清除音乐历史记录成功！", Toast.LENGTH_SHORT)
                    .show();
            MusicUtil.clearMusicHistories(this);
            // 隐藏历史记录列表，代替刷新，减少开销
            binding.rvHistory.setVisibility(View.GONE);
        });
    }

}
